package 字符串与String的相互转换;

/**
 * @author feng
 * @Date 2022/1/2 9:55
 * 一个子串在整串中出现的次数。
 */
public class Test2 {
    public static void main(String[] args) {
        String str = "nbaernbatynbauinbaopnba";
        String key = "nba";
        int count = getKeyStringCount(str, key);
        System.out.println("次数" + count);
    }

    private static int getKeyStringCount(String str, String key) {
        int count = 0;
        int index = 0;
        //返回某个字符串出现的第一个下标
        while ((index = str.indexOf(key, index)) != -1) {
            index = key.length() + index;
            count++;
        }
        return count;
    }
}
